package com.sh.data.engine.domain.annotation;

import com.sh.data.engine.domain.authority.service.impl.StatisticEntranceService;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ProjectStatisticAspect {
    private static final Logger log = LoggerFactory.getLogger(ProjectStatisticAspect.class);

    @Autowired
    private StatisticEntranceService statisticEntranceService;

    @Pointcut("@annotation(com.sh.data.engine.domain.annotation.ProjectStatisticExecution)")
    public void myProjectStatisticExecution() {
    }

    @Around("myProjectStatisticExecution()")
    public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        ProjectStatisticExecution execution = signature.getMethod().getAnnotation(ProjectStatisticExecution.class);
        Object result;
        try {
            Long before = statisticEntranceService.executionCount(execution.type().getType());//对应统计实体表记录数
            result = joinPoint.proceed(); // 继续执行方法
            Long after = statisticEntranceService.executionCount(execution.type().getType());
            statisticEntranceService.executionUpdate(execution.type().getType(), before, after);//对应更新tb_project_statistics字段值
            log.info("update tb_project_statistic success");
        } catch (Exception e) {
            throw e;
        }
        return result;
    }

}
